
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="商品类型:" prop="goodType">
    <el-select v-model="formData.goodType" placeholder="请选择商品类型" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in GoodsClassOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="商品编码:" prop="goodCode">
    <el-input v-model="formData.goodCode" :clearable="true" placeholder="请输入商品编码" />
</el-form-item>
        <el-form-item label="商品名称:" prop="goodName">
    <el-input v-model="formData.goodName" :clearable="true" placeholder="请输入商品名称" />
</el-form-item>
        <el-form-item label="是否添加视频">
          <el-switch @click="addVideos=!addVideos"/>
        </el-form-item>
        <!--              <el-form-item label="视频链接:" prop="videoUrl">-->
        <!--                <el-input v-model="formData.videoUrl" :clearable="true" placeholder="请输入视频链接" />-->
        <!--              </el-form-item>-->
        <el-form-item label="视频链接" label-width="80px" v-if="addVideos==true">
          <SelectImage v-model="formData.videoUrl" />
        </el-form-item>
        <el-form-item label="品牌ID:" prop="brandId">
    <el-input v-model.number="formData.brandId" :clearable="true" placeholder="请输入品牌ID" />
</el-form-item>
        <el-form-item label="商品单位:" prop="goodUnit">
    <el-select v-model="formData.goodUnit" placeholder="请选择商品单位" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in GoodsOnceOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="供应商ID:" prop="suppilerId">
    <el-input v-model.number="formData.suppilerId" :clearable="true" placeholder="请输入供应商ID" />
</el-form-item>
        <el-form-item label="是否多规格:" prop="isManySpec">
    <el-select v-model="formData.isManySpec" placeholder="请选择是否多规格" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="价格:" prop="price">
    <el-input-number v-model="formData.price" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="划线价:" prop="marketPrice">
    <el-input-number v-model="formData.marketPrice" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="成本价:" prop="costPrice">
    <el-input-number v-model="formData.costPrice" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="库存:" prop="stock">
    <el-input v-model.number="formData.stock" :clearable="true" placeholder="请输入库存" />
</el-form-item>
        <el-form-item label="重量:" prop="weight">
    <el-input-number v-model="formData.weight" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="体积:" prop="volume">
    <el-input-number v-model="formData.volume" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="条码:" prop="barCode">
    <el-input v-model="formData.barCode" :clearable="true" placeholder="请输入条码" />
</el-form-item>
        <el-form-item label="物流方式:" prop="logisticsWay">
    <el-select v-model="formData.logisticsWay" placeholder="请选择物流方式" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in wayOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="运费设置:" prop="shipFeeSet">
    <el-select v-model="formData.shipFeeSet" placeholder="请选择运费设置" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="运费:" prop="shipFee">
    <el-input-number v-model="formData.shipFee" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="商品详情:" prop="goodDetail">
    <el-input v-model="formData.goodDetail" :clearable="true" placeholder="请输入商品详情" />
</el-form-item>
        <el-form-item label="库存预警:" prop="stockWarn">
    <el-input v-model.number="formData.stockWarn" :clearable="true" placeholder="请输入库存预警" />
</el-form-item>
        <el-form-item label="虚拟销量:" prop="virtualSaleNum">
    <el-input v-model.number="formData.virtualSaleNum" :clearable="true" placeholder="请输入虚拟销量" />
</el-form-item>
        <el-form-item label="虚拟浏览量:" prop="virtualBrowseNum">
    <el-input v-model.number="formData.virtualBrowseNum" :clearable="true" placeholder="请输入虚拟浏览量" />
</el-form-item>
        <el-form-item label="限购方式:" prop="limitWay">
    <el-select v-model="formData.limitWay" placeholder="请选择限购方式" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="限购数量:" prop="limitNum">
    <el-input v-model.number="formData.limitNum" :clearable="true" placeholder="请输入限购数量" />
</el-form-item>
        <el-form-item label="商品上架状态:" prop="goodStatus">
    <el-select v-model="formData.goodStatus" placeholder="请选择商品上架状态" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="商品筛选状态:" prop="goodSearchType">
    <el-select v-model="formData.goodSearchType" placeholder="请选择商品筛选状态" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in switchOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="排序:" prop="sort">
    <el-input v-model.number="formData.sort" :clearable="true" placeholder="请输入排序" />
</el-form-item>
        <el-form-item label="classifyId字段:" prop="classifyId">
    <el-input v-model.number="formData.classifyId" :clearable="true" placeholder="请输入classifyId字段" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createLsGoods,
  updateLsGoods,
  findLsGoods
} from '@/api/goods/lsGoods'

defineOptions({
    name: 'LsGoodsForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import SelectImage from "@/components/selectImage/selectImage.vue";


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const GoodsClassOptions = ref([])
const GoodsOnceOptions = ref([])
const wayOptions = ref([])
const switchOptions = ref([])
const addVideos=false
const formData = ref({
            goodType: '',
            goodCode: '',
            goodName: '',
            isVideo: '',
            brandId: undefined,
            goodUnit: '',
            suppilerId: undefined,
            isManySpec: '',
            price: 0,
            marketPrice: 0,
            costPrice: 0,
            stock: undefined,
            weight: 0,
            volume: 0,
            barCode: '',
            logisticsWay: '',
            shipFeeSet: '',
            shipFee: 0,
            goodDetail: '',
            stockWarn: undefined,
            virtualSaleNum: undefined,
            virtualBrowseNum: undefined,
            limitWay: '',
            limitNum: undefined,
            goodStatus: '',
            goodSearchType: '',
            sort: undefined,
            videoUrl: '',
            classifyId: undefined,
        })
// 验证规则
const rule = reactive({
               goodType : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodCode : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodName : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               isVideo : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               brandId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodUnit : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               suppilerId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               isManySpec : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               price : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               marketPrice : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               costPrice : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               stock : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               weight : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               volume : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               barCode : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               logisticsWay : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               shipFeeSet : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodDetail : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               limitWay : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodStatus : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findLsGoods({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    GoodsClassOptions.value = await getDictFunc('GoodsClass')
    GoodsOnceOptions.value = await getDictFunc('GoodsOnce')
    wayOptions.value = await getDictFunc('way')
    switchOptions.value = await getDictFunc('switch')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createLsGoods(formData.value)
               break
             case 'update':
               res = await updateLsGoods(formData.value)
               break
             default:
               res = await createLsGoods(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
